package com.jz.kufangkuqu.kufang.controller;


import com.jz.kufangkuqu.kufang.bean.KuFangAdd;
import com.jz.kufangkuqu.kufang.bean.KuFangSelect;
import com.jz.kufangkuqu.kufang.bean.KuFangUpdate;
import com.jz.kufangkuqu.kufang.bean.KuFangUpdatestatus;
import com.jz.kufangkuqu.kufang.service.KuFangService;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@RestController
@CrossOrigin
public class KuFangController {

    @Autowired
    private KuFangService kuFangService;

    //分页
    @PostMapping("warehouse/queryPage")
    public ResultVO querypage(@RequestBody KuFangSelect kuFangSelectVo){
        if(kuFangSelectVo.getPageIndex()==null){
            kuFangSelectVo.setPageIndex(1);
        }
        if(kuFangSelectVo.getPageSize()==null){
            kuFangSelectVo.setPageSize(10);
        }
        ResultVO resultVO = kuFangService.queryPage(kuFangSelectVo);
        return resultVO;
    }

    //新增
    @PostMapping("warehouse/add")
    public ResultVO add(@RequestBody @Valid KuFangAdd kuFangAdd, BindingResult bindingResult ){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = kuFangService.add(kuFangAdd);
        return resultVO;
    }

    //修改
    @PostMapping("warehouse/update")
    public ResultVO update(@RequestBody @Valid KuFangUpdate kuFangUpdate, BindingResult bindingResult ){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = kuFangService.update(kuFangUpdate);
        return resultVO;
    }
    //启用禁用
    @PostMapping("warehouse/updateStatus")
    public ResultVO updateStatus(@RequestBody @Valid KuFangUpdatestatus kuFangUpdatestatus, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return kuFangService.updateStatus(kuFangUpdatestatus);
    }
}